package com.wchat.serviceimpl;

import com.wchat.dto.OrderDto;
import com.wchat.enums.ResultEnum;
import com.wchat.exceptions.SellException;
import com.wchat.service.BuyerService;
import com.wchat.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by yangtianrui on 17/8/13.
 */
@Service
@Slf4j
public class BuyerServiceImpl implements BuyerService {

    @Autowired
    private OrderService orderService;

    @Override
    public OrderDto findOrderOne(String orderId, String openId) {
        return checkOrder(orderId,openId);
    }
    @Override
    public OrderDto cancelOrder(String orderId, String openId) {

        OrderDto orderDto = checkOrder(orderId,openId);
        if(orderDto == null)
        {
            log.error("取消订单] 错误{}",orderId);
            throw  new SellException(ResultEnum.ORDER_NOT_EXIT);
        }
        return orderService.cancel(orderDto);
    }
    public OrderDto checkOrder(String orderId,String openId)
    {
        OrderDto orderDto = orderService.findOne(orderId);
        if(!orderDto.getBuyerOpenid().equalsIgnoreCase(openId))
        {
            log.error("[查询订单] 订单查询错误{}",orderId);
            throw new SellException(ResultEnum.ORDER_MASTER_ERROR);
        }
        return orderDto;
    }
}
